package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.OrderPayments;
import com.example.mall.dto.OrderPaymentsDto;
import com.example.mall.mapper.OrderPaymentsMapper;
import com.example.mall.query.OrderPaymentsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.IOrderPaymentsService;
import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单支付记录表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-09
 */
@Service
public class OrderPaymentsServiceImpl extends ServiceImpl<OrderPaymentsMapper, OrderPayments> implements IOrderPaymentsService {

    @Autowired
    private OrderPaymentsMapper orderPaymentsMapper;

    @Override
    public PageDTO<OrderPaymentsDto> queryPage(OrderPaymentsQuery orderPaymentsQuery) {
        if (orderPaymentsQuery == null) {
            orderPaymentsQuery = new OrderPaymentsQuery();
        }
        Page<OrderPayments> page = orderPaymentsQuery.toMpPageByDefaultCreateTimeAsc();
        Page<OrderPayments> cartPage = lambdaQuery()
                .like(orderPaymentsQuery.getOrderId()!= null, OrderPayments::getOrderId, orderPaymentsQuery.getOrderId())
                .like(orderPaymentsQuery.getPaymentId()!= null, OrderPayments::getPaymentId, orderPaymentsQuery.getPaymentId())
                .like(orderPaymentsQuery.getPaymentMethod()!= null, OrderPayments::getPaymentMethod, orderPaymentsQuery.getPaymentMethod())
                .like(orderPaymentsQuery.getPaymentStatus()!= null, OrderPayments::getPaymentStatus, orderPaymentsQuery.getPaymentStatus())
                .like(orderPaymentsQuery.getAmount()!= null, OrderPayments::getAmount, orderPaymentsQuery.getAmount())
                .like(orderPaymentsQuery.getTransactionId()!= null, OrderPayments::getTransactionId, orderPaymentsQuery.getTransactionId())
                .orderByDesc(OrderPayments::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, OrderPaymentsDto.class);
    }

    @Override
    public OrderPayments queryOrderPaymentsById(Long paymentId) {
        AssertUtil.isNotNull(paymentId, "订单支付ID不能为空");
        return orderPaymentsMapper.selectById(paymentId);
    }

    @Override
    public List<OrderPayments> queryOrderPaymentsList() {
        return this.list();
    }

    @Override
    public void update(OrderPaymentsDto orderPaymentsDto) {
        AssertUtil.isNotNull(orderPaymentsDto.getPaymentId(), "订单支付ID不能为空");
        OrderPayments orderPayments = this.queryOrderPaymentsById(orderPaymentsDto.getPaymentId());
        AssertUtil.isNotNull(orderPayments, "订单支付不存在");
        CopyUtils.copyProperties(orderPaymentsDto, orderPayments);
        orderPayments.setUpdateDate(new Date());
        orderPaymentsMapper.updateById(orderPayments);
    }

    @Override
    public void insert(OrderPaymentsDto orderPaymentsDto) {
        OrderPayments orderPayments = new OrderPayments();
        CopyUtils.copyProperties(orderPaymentsDto, orderPayments);
        orderPayments.setPaymentId(CodeGenerateUtils.generateUniqueId());
        orderPayments.setCreateDate(new Date());
        orderPayments.setUpdateDate(new Date());
        orderPayments.setPaymentStatus(BaseConstants.NOT_USED);
        orderPaymentsMapper.insert(orderPayments);
    }

    @Override
    public void delete(Long paymentId) {
        AssertUtil.isNotNull(paymentId, "订单支付ID不能为空");
        OrderPayments orderPayments = orderPaymentsMapper.selectById(paymentId);
        AssertUtil.isNotNull(orderPayments, "订单支付不存在");
        orderPayments.setUpdateDate(new Date());
        orderPayments.setPaymentStatus(BaseConstants.CANCEL);
        orderPaymentsMapper.updateById(orderPayments);
    }

    @Override
    public void deleteBatch(List<Long> paymentIds) {
        AssertUtil.isNotNull(paymentIds, "订单支付ID列表不能为空");
        List<OrderPayments> orderPayments = orderPaymentsMapper.selectBatchIds(paymentIds);
        AssertUtil.isNotNull(orderPayments, "订单支付不存在");
        orderPayments.forEach(orderPayment -> {
            orderPayment.setUpdateDate(new Date());
            orderPayment.setPaymentStatus(BaseConstants.CANCEL);
            orderPaymentsMapper.updateById(orderPayment);
        });
    }
}
